//-----------------------------------------------------------------------
// <copyright file="ProductSellDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class ProductSellDAOBase
    {
        #region Common methods
        public virtual ProductSell CreateProductSellFromReader(IDataReader reader)
        {
            ProductSell item = new ProductSell();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ProductId"))) item.ProductId = (long)reader["ProductId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Url"))) item.Url = (string)reader["Url"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("ProductDescription"))) item.ProductDescription = (string)reader["ProductDescription"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) item.CreatedDate = (DateTime)reader["CreatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("UpdatedDate"))) item.UpdatedDate = (DateTime)reader["UpdatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("MemberId"))) item.MemberId = (long)reader["MemberId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Order"))) item.Order = (long)reader["Order"];
                if (!reader.IsDBNull(reader.GetOrdinal("Image"))) item.Image = (string)reader["Image"];
                if (!reader.IsDBNull(reader.GetOrdinal("ThumbnailImage"))) item.ThumbnailImage = (string)reader["ThumbnailImage"];
                if (!reader.IsDBNull(reader.GetOrdinal("EndDate"))) item.EndDate = (DateTime)reader["EndDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("CatId"))) item.CatId = (long)reader["CatId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Viewed"))) item.Viewed = (long)reader["Viewed"];
                if (!reader.IsDBNull(reader.GetOrdinal("ObjectId"))) item.ObjectId = (long)reader["ObjectId"];
                if (!reader.IsDBNull(reader.GetOrdinal("StatusId"))) item.StatusId = (int)reader["StatusId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
                if (!reader.IsDBNull(reader.GetOrdinal("ActivateCode"))) item.ActivateCode = (string)reader["ActivateCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("CountryId"))) item.CountryId = (long)reader["CountryId"];
                if (!reader.IsDBNull(reader.GetOrdinal("MinOrderQuantity"))) item.MinOrderQuantity = (string)reader["MinOrderQuantity"];
                if (!reader.IsDBNull(reader.GetOrdinal("MinQuantityTypeId"))) item.MinQuantityTypeId = (long)reader["MinQuantityTypeId"];
                if (!reader.IsDBNull(reader.GetOrdinal("MinQuantityOther"))) item.MinQuantityOther = (string)reader["MinQuantityOther"];
                if (!reader.IsDBNull(reader.GetOrdinal("FOBPriceId"))) item.FOBPriceId = (long)reader["FOBPriceId"];
                if (!reader.IsDBNull(reader.GetOrdinal("PriceSmall"))) item.PriceSmall = (byte[])reader["PriceSmall"];
                if (!reader.IsDBNull(reader.GetOrdinal("PriceLarge"))) item.PriceLarge = (byte[])reader["PriceLarge"];
                if (!reader.IsDBNull(reader.GetOrdinal("PerTypeId"))) item.PerTypeId = (long)reader["PerTypeId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Port"))) item.Port = (string)reader["Port"];
                if (!reader.IsDBNull(reader.GetOrdinal("PaymentTerms"))) item.PaymentTerms = (string)reader["PaymentTerms"];
                if (!reader.IsDBNull(reader.GetOrdinal("Production"))) item.Production = (string)reader["Production"];
                if (!reader.IsDBNull(reader.GetOrdinal("DeliveryTime"))) item.DeliveryTime = (string)reader["DeliveryTime"];
                if (!reader.IsDBNull(reader.GetOrdinal("PackagingDetail"))) item.PackagingDetail = (string)reader["PackagingDetail"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateProductSellFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateProductSell methods
            
        public virtual void CreateProductSell(ProductSell productSell)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductSellInsert");
                
                database.AddInParameter(dbCommand, "@Url", DbType.String, productSell.Url);
                database.AddInParameter(dbCommand, "@Title", DbType.String, productSell.Title);
                database.AddInParameter(dbCommand, "@ProductDescription", DbType.String, productSell.ProductDescription);
                database.AddInParameter(dbCommand, "@Description", DbType.String, productSell.Description);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, productSell.CreatedDate);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, productSell.UpdatedDate);
                database.AddInParameter(dbCommand, "@MemberId", DbType.Int64, productSell.MemberId);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, productSell.Order);
                database.AddInParameter(dbCommand, "@Image", DbType.String, productSell.Image);
                database.AddInParameter(dbCommand, "@ThumbnailImage", DbType.String, productSell.ThumbnailImage);
                database.AddInParameter(dbCommand, "@EndDate", DbType.DateTime, productSell.EndDate);
                database.AddInParameter(dbCommand, "@CatId", DbType.Int64, productSell.CatId);
                database.AddInParameter(dbCommand, "@Viewed", DbType.Int64, productSell.Viewed);
                database.AddInParameter(dbCommand, "@ObjectId", DbType.Int64, productSell.ObjectId);
                database.AddInParameter(dbCommand, "@StatusId", DbType.Int32, productSell.StatusId);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, productSell.Enable);
                database.AddInParameter(dbCommand, "@ActivateCode", DbType.String, productSell.ActivateCode);
                database.AddInParameter(dbCommand, "@CountryId", DbType.Int64, productSell.CountryId);
                database.AddInParameter(dbCommand, "@MinOrderQuantity", DbType.String, productSell.MinOrderQuantity);
                database.AddInParameter(dbCommand, "@MinQuantityTypeId", DbType.Int64, productSell.MinQuantityTypeId);
                database.AddInParameter(dbCommand, "@MinQuantityOther", DbType.String, productSell.MinQuantityOther);
                database.AddInParameter(dbCommand, "@FOBPriceId", DbType.Int64, productSell.FOBPriceId);
                database.AddInParameter(dbCommand, "@PriceSmall", DbType.Binary, productSell.PriceSmall);
                database.AddInParameter(dbCommand, "@PriceLarge", DbType.Binary, productSell.PriceLarge);
                database.AddInParameter(dbCommand, "@PerTypeId", DbType.Int64, productSell.PerTypeId);
                database.AddInParameter(dbCommand, "@Port", DbType.String, productSell.Port);
                database.AddInParameter(dbCommand, "@PaymentTerms", DbType.String, productSell.PaymentTerms);
                database.AddInParameter(dbCommand, "@Production", DbType.String, productSell.Production);
                database.AddInParameter(dbCommand, "@DeliveryTime", DbType.String, productSell.DeliveryTime);
                database.AddInParameter(dbCommand, "@PackagingDetail", DbType.String, productSell.PackagingDetail);
                database.AddOutParameter(dbCommand, "@ProductId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                productSell.ProductId = (long)database.GetParameterValue(dbCommand, "@ProductId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateProductSellException, ex);
            }
        }

        #endregion

        #region UpdateProductSell methods
        
        public virtual void UpdateProductSell(ProductSell productSell)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductSellUpdate");            
                
                database.AddInParameter(dbCommand, "@ProductId", DbType.Int64, productSell.ProductId);
                database.AddInParameter(dbCommand, "@Url", DbType.String, productSell.Url);
                database.AddInParameter(dbCommand, "@Title", DbType.String, productSell.Title);
                database.AddInParameter(dbCommand, "@ProductDescription", DbType.String, productSell.ProductDescription);
                database.AddInParameter(dbCommand, "@Description", DbType.String, productSell.Description);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, productSell.CreatedDate);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, productSell.UpdatedDate);
                database.AddInParameter(dbCommand, "@MemberId", DbType.Int64, productSell.MemberId);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, productSell.Order);
                database.AddInParameter(dbCommand, "@Image", DbType.String, productSell.Image);
                database.AddInParameter(dbCommand, "@ThumbnailImage", DbType.String, productSell.ThumbnailImage);
                database.AddInParameter(dbCommand, "@EndDate", DbType.DateTime, productSell.EndDate);
                database.AddInParameter(dbCommand, "@CatId", DbType.Int64, productSell.CatId);
                database.AddInParameter(dbCommand, "@Viewed", DbType.Int64, productSell.Viewed);
                database.AddInParameter(dbCommand, "@ObjectId", DbType.Int64, productSell.ObjectId);
                database.AddInParameter(dbCommand, "@StatusId", DbType.Int32, productSell.StatusId);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, productSell.Enable);
                database.AddInParameter(dbCommand, "@ActivateCode", DbType.String, productSell.ActivateCode);
                database.AddInParameter(dbCommand, "@CountryId", DbType.Int64, productSell.CountryId);
                database.AddInParameter(dbCommand, "@MinOrderQuantity", DbType.String, productSell.MinOrderQuantity);
                database.AddInParameter(dbCommand, "@MinQuantityTypeId", DbType.Int64, productSell.MinQuantityTypeId);
                database.AddInParameter(dbCommand, "@MinQuantityOther", DbType.String, productSell.MinQuantityOther);
                database.AddInParameter(dbCommand, "@FOBPriceId", DbType.Int64, productSell.FOBPriceId);
                database.AddInParameter(dbCommand, "@PriceSmall", DbType.Binary, productSell.PriceSmall);
                database.AddInParameter(dbCommand, "@PriceLarge", DbType.Binary, productSell.PriceLarge);
                database.AddInParameter(dbCommand, "@PerTypeId", DbType.Int64, productSell.PerTypeId);
                database.AddInParameter(dbCommand, "@Port", DbType.String, productSell.Port);
                database.AddInParameter(dbCommand, "@PaymentTerms", DbType.String, productSell.PaymentTerms);
                database.AddInParameter(dbCommand, "@Production", DbType.String, productSell.Production);
                database.AddInParameter(dbCommand, "@DeliveryTime", DbType.String, productSell.DeliveryTime);
                database.AddInParameter(dbCommand, "@PackagingDetail", DbType.String, productSell.PackagingDetail);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateProductSellException, ex);
            }
        }
        
        #endregion

        #region DeleteProductSell methods
        public virtual void DeleteProductSell(long productId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductSellDelete");
                
                database.AddInParameter(dbCommand, "@ProductId", DbType.Int64, productId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteProductSellException, ex);
            }
        }

        #endregion

        #region GetProductSell methods
        
        public virtual ProductSell GetProductSell(long productId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductSellGet");
                
                database.AddInParameter(dbCommand, "@ProductId", DbType.Int64, productId);
                
                ProductSell productSell =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        productSell = CreateProductSellFromReader(reader);
                        reader.Close();
                    }
                }
                return productSell;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetProductSellException, ex);
            }
        }
        
        #endregion

        #region GetProductSellList methods
        public virtual ProductSellCollection GetProductSellList(ProductSellColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductSellGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                ProductSellCollection productSellCollection = new ProductSellCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        ProductSell productSell = CreateProductSellFromReader(reader);
                        productSellCollection.Add(productSell);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return productSellCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetProductSellListException, ex);
            }
        }
        
        public virtual ProductSellCollection GetProductSellList(ProductSellColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetProductSellList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetProductSellList methods
        public virtual ProductSellCollection GetProductSellListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductSellGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                ProductSellCollection productSellCollection = new ProductSellCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        ProductSell productSell = CreateProductSellFromReader(reader);
                        productSellCollection.Add(productSell);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return productSellCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetProductSellListException, ex);
            }
        }
        
        public virtual ProductSellCollection GetProductSellListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetProductSellListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}